// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
#pragma once

#include <melon/container/f14_set.h>
#include <pollux/functions/udf.h>

namespace kumo::pollux::functions::sparksql {
    template<typename T>
    struct StringToMapFunction {
        POLLUX_DEFINE_FUNCTION_TYPES(T);

        // Results refer to strings in the first argument.
        static constexpr int32_t reuse_strings_from_arg = 0;

        void call(
            out_type<Map<Varchar, Varchar> > &out,
            const arg_type<Varchar> &input,
            const arg_type<Varchar> &entryDelimiter,
            const arg_type<Varchar> &keyValueDelimiter) {
            POLLUX_USER_CHECK_EQ(
                entryDelimiter.size(), 1, "entryDelimiter's size should be 1.");
            POLLUX_USER_CHECK_EQ(
                keyValueDelimiter.size(), 1, "keyValueDelimiter's size should be 1.");

            callImpl(
                out,
                toStringView(input),
                toStringView(entryDelimiter),
                toStringView(keyValueDelimiter));
        }

    private:
        static std::string_view toStringView(const arg_type<Varchar> &input) {
            return std::string_view(input.data(), input.size());
        }

        void callImpl(
            out_type<Map<Varchar, Varchar> > &out,
            std::string_view input,
            std::string_view entryDelimiter,
            std::string_view keyValueDelimiter) const {
            size_t pos = 0;
            melon::F14FastSet<std::string_view> keys;

            auto nextEntryPos = input.find(entryDelimiter, pos);
            while (nextEntryPos != std::string::npos) {
                processEntry(
                    out,
                    std::string_view(input.data() + pos, nextEntryPos - pos),
                    keyValueDelimiter,
                    keys);

                pos = nextEntryPos + 1;
                nextEntryPos = input.find(entryDelimiter, pos);
            }

            processEntry(
                out,
                std::string_view(input.data() + pos, input.size() - pos),
                keyValueDelimiter,
                keys);
        }

        void processEntry(
            out_type<Map<Varchar, Varchar> > &out,
            std::string_view entry,
            std::string_view keyValueDelimiter,
            melon::F14FastSet<std::string_view> &keys) const {
            const auto delimiterPos = entry.find(keyValueDelimiter, 0);
            // Allows keyValue delimiter not found.
            if (delimiterPos == std::string::npos) {
                out.add_null().setNoCopy(StringView(entry));
                return;
            }
            const auto key = std::string_view(entry.data(), delimiterPos);
            POLLUX_USER_CHECK(
                keys.insert(key).second, "Duplicate keys are not allowed: '{}'.", key);
            const auto value = StringView(
                entry.data() + delimiterPos + 1, entry.size() - delimiterPos - 1);

            auto [keyWriter, valueWriter] = out.add_item();
            keyWriter.setNoCopy(StringView(key));
            valueWriter.setNoCopy(value);
        }
    };
} // namespace kumo::pollux::functions::sparksql
